const NewsModel = require('../../models/NewsModel')



const NewsService = {
    add: async ({ title, content, category, cover, publish, editTime, author }) => {
        // console.log("数据库的模型处理");
        try {
            return NewsModel.create({ title, content, category, cover, publish, editTime, author })
        } catch (error) {
            throw error
        }

    },
    update: async ({ _id, title, content, category, publish, cover, editTime }) => {
        try {
            if (cover) {
                return NewsModel.updateOne({ _id }, { title, content, category, publish, cover, editTime })
            } else {
                return NewsModel.updateOne({ _id }, { title, content, category, publish, editTime })
            }
        } catch (error) {
            throw error
        }
    },
    getListByAuthor: async ({ author }) => {
        return NewsModel.find({ author })
    },
    getListById: async ({ _id }) => {
        return NewsModel.find({ _id })

    },
    publish: async ({ _id, publish, editTime }) => {
        try {
            return NewsModel.updateOne({ _id }, { publish, editTime })
        } catch (error) {
            throw error
        }
    },
    delete: async ({ _id }) => {
        try {
            return NewsModel.deleteOne({ _id })
        } catch (error) {
            throw error
        }
    }
}




module.exports = NewsService